package au.com.iglootech.TaxTimeToolTip.ui.feerate;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.FeeRate;
import au.com.iglootech.TaxTimeToolTip.dao.GenericDao;
import au.com.iglootech.TaxTimeToolTip.ui.company.CompanyFieldsFactory;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@Configurable(preConstruction = true)
@Component
@Scope("request")
public class CreateFeeRateTab extends CustomComponent {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final static Logger logger = Logger
			.getLogger(CreateFeeRateTab.class.getName());

	VerticalLayout mainLayout = new VerticalLayout();
	@Autowired
	private transient GenericDao genericDao;
	private FeeRate feeRate;
	private BeanItem<FeeRate> personItem;

	public CreateFeeRateTab() {
		setCaption("Add New Task");
		feeRate = new FeeRate();
		initContent();
	}

	private void initContent() {
		Panel panel = new Panel();
		final Form feeRateForm = new Form();

		personItem = new BeanItem<FeeRate>(feeRate);
		feeRateForm.setCaption("Personal details");
		feeRateForm.setWriteThrough(false); // we want explicit 'apply'
		feeRateForm.setInvalidCommitted(false); // no invalid values in
												// datamodel

		// FieldFactory for customizing the fields and adding validators
		feeRateForm.setFormFieldFactory(new FeeRateFieldFactory());
		feeRateForm.setItemDataSource(personItem); // bind to POJO via BeanItem

		// Determines which properties are shown, and in which order:
		feeRateForm.setVisibleItemProperties(Arrays
				.asList(new String[] { "rate" }));
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		Button apply = new Button("Apply", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					feeRateForm.commit();
					genericDao.save(feeRate);
					getWindow().showNotification("You fee rate has been saved.");
					feeRate = new FeeRate();
					feeRateForm.discard();
					personItem = new BeanItem<FeeRate>(feeRate);
					feeRateForm.setItemDataSource(personItem);
					feeRateForm.setVisibleItemProperties(Arrays
							.asList(new String[] { "rate" }));
				} catch (Exception e) {
					// Ignored, we'll let the Form handle the errors
					logger.error(e);
				}
			}
		});
		buttons.addComponent(apply);
		feeRateForm.getFooter().addComponent(buttons);
		feeRateForm.getFooter().setMargin(false, false, true, true);
		panel.addComponent(feeRateForm);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}
}
